package com.czh.gzskzn.qqkonjian.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.czh.gzskzn.qqkonjian.MainActivity;
import com.czh.gzskzn.qqkonjian.R;
import com.czh.gzskzn.qqkonjian.UtilServer.WanZhi;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;


public class LoginActivity extends AppCompatActivity {

    private Button denglu;
    private EditText yonhumin,mima;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
            intinView();


             DianJie();
    }

    private void DianJie() {

        denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String yonhu = yonhumin.getText().toString();
                String mi = mima.getText().toString();
        if (yonhu.equals("")&&mi.equals("")){

            Toast.makeText(LoginActivity.this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
        }else {
            OkGo.get(WanZhi.BIAODAN + "username=" + yonhu + "&password=" + mi)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, okhttp3.Response response) {
                            Log.e("dddddddd", s);
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                String caty = jsonObject.getString("result");
                                Log.e("sssss", caty);
                                if (caty.equals("1")) {
                                    Intent i = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(i);

                                } else {
                                    Toast.makeText(LoginActivity.this, "用户名或者密码错误！！！", Toast.LENGTH_SHORT).show();
                                    yonhumin.setText("");
                                    mima.setText("");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        }
                    });
            }
            }

        });;
    }

    private void intinView() {
        denglu = (Button) findViewById(R.id.denglu);
        yonhumin = (EditText) findViewById(R.id.yonhu);
        mima = (EditText) findViewById(R.id.mima);
    }
}
